package com.kevin.design.structuralPattern.composite;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CompositeOfKillVirus {
    public static void main(String[] args) {
        Folder imageFolder = new Folder("图像文件夹");
        Folder txtFolder = new Folder("文本文件夹");
        Folder folder = new Folder("src");

        ImageFile imageFile1 = new ImageFile("1.jpg");
        ImageFile imageFile2 = new ImageFile("2.png");

        TxTFile txTFile1 = new TxTFile("文本1.doc");
        TxTFile txTFile2 = new TxTFile("文本2.txt");

        txtFolder.add(txTFile1);
        txtFolder.add(txTFile2);
        imageFolder.add(imageFile1);
        imageFolder.add(imageFile2);
        folder.add(txtFolder);
        folder.add(imageFolder);

        imageFolder.killVirus();
        System.out.println("---------------------------------------");
        txtFolder.killVirus();
        System.out.println("----------------------------------------");
        folder.killVirus();

     }
}

abstract class AbstractFile{
    abstract void add(AbstractFile abstractFile);
    abstract void remove(AbstractFile abstractFile);
    abstract void killVirus();
}

class Folder extends AbstractFile{
    private String folderName;

    public Folder(String folderName) {
        this.folderName = folderName;
    }

    private List<AbstractFile> fileList = new ArrayList<>();
    @Override
    void add(AbstractFile abstractFile) {
        fileList.add(abstractFile);
    }

    @Override
    void remove(AbstractFile abstractFile) {
        fileList.remove(abstractFile);
    }

    @Override
    void killVirus() {
        System.out.println("现在对"+folderName+"文件夹进行查杀");
        for (Object o:fileList) {
            ((AbstractFile)o).killVirus();
        }
    }
}

class ImageFile extends AbstractFile{
    private String imageName;

    public ImageFile(String imageName) {
        this.imageName = imageName;
    }

    @Override
    void add(AbstractFile abstractFile) {

    }

    @Override
    void remove(AbstractFile abstractFile) {

    }

    @Override
    void killVirus() {
        System.out.println("现在开始对"+imageName+"图像文件进行查杀");
    }
}

class TxTFile extends AbstractFile{
    private String txtFileName;

    public TxTFile(String txtFileName) {
        this.txtFileName = txtFileName;
    }

    @Override
    void add(AbstractFile abstractFile) {

    }

    @Override
    void remove(AbstractFile abstractFile) {

    }

    @Override
    void killVirus() {
        System.out.println("现在开始对"+txtFileName+"文本文件进行查杀");
    }
}